package com.jian.mall.controller;

import com.jian.mall.common.to.elasticsearch.SkuEsTo;
import com.jian.mall.common.utils.R;
import com.jian.mall.service.ElasticsearchService;
import com.jian.mall.vo.SearchConditionVo;
import com.jian.mall.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/search/search")
public class ElasticsearchController {

    @Autowired
    private ElasticsearchService elasticsearchService;

    @PostMapping("/produce/save")
    public R saveProduct(@RequestBody List<SkuEsTo> skuEsTo) {
        try {
            elasticsearchService.saveBatch(skuEsTo);
        } catch (IOException e) {
            return R.error().put("error","商品上架错误");
        }
        return R.ok();
    }

    @GetMapping("/test")
    public String test(){
        return "哈哈哈哈哈";
    }

    @GetMapping("/indexData")
    public SearchResult searchResult(SearchConditionVo conditionVo){
        return elasticsearchService.byConditionVoSearch(conditionVo);
    }

}
